package com.czp.ssm.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.czp.ssm.bean.Opera;
import com.czp.ssm.bean.OperaRule;
import com.czp.ssm.bean.PlaneOrder;
import com.czp.ssm.bean.User;
import com.czp.ssm.dao.OperaMapper;
import com.czp.ssm.dao.OperaRuleMapper;
import com.czp.ssm.dao.PlaneOrderMapper;
import com.czp.ssm.dao.UserMapper;
import com.czp.ssm.service.OperaService;

@Service
public class OperaServiceImpl implements OperaService {

	@Autowired
	private OperaMapper operaMapper;
	@Autowired
	private PlaneOrderMapper planeOrderMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private OperaRuleMapper operaRuleMapper;
	public Opera getOperaPersonIdByCompanyIdAndUserId(String COMPANY_ID, String USER_ID) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("COMPANY_ID", COMPANY_ID);
		map.put("USER_ID", USER_ID);
		Opera opera = operaMapper.selectOperaPersonIdByCompanyIdAndUserId(map);
		System.out.println("operaPersonId----"+opera.getOperaPersonId());
		return opera;
	}

	public List<Opera> getOperaByOperaPersonId(String operaPersonId) {
		List<Opera> operaList = operaMapper.selectOperaByOperaPersonId(operaPersonId);
		return operaList;
	}

	public List<PlaneOrder> findPlaneOrderByOperaPersonId(String operaPersonId) {
		List<PlaneOrder> planeOrders = new ArrayList<PlaneOrder>();
		List<Opera> operas = operaMapper.selectOperaByOperaPersonId(operaPersonId);
		for (int i = 0; i < operas.size(); i++) {
			Opera opera = operas.get(i);
			List<PlaneOrder> planeOrderList = planeOrderMapper.selectPlaneOrderListByOpera(opera);
			if (planeOrderList != null && planeOrderList.size()>0) {
				for (int j = 0; j < planeOrderList.size(); j++) {
					PlaneOrder planeOrder = planeOrderList.get(j);
					User user = userMapper.selectUserByUserId(opera.getUserId());
					planeOrder.setLinkPhone(user.getUserPhone());
					OperaRule operaRule = operaRuleMapper.selectByOperaRuleId(opera.getOperaRuleId());
					planeOrder.setOperaRuleContent(operaRule.getOperaRuleContent());
					planeOrders.add(planeOrder);
				}
			}
		}
		return planeOrders;
	}

}
